﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.DAL.Gateway;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class SupplierPaymentEntry : System.Web.UI.Page
    {
        private SupplierPaymentManager aManager = new SupplierPaymentManager();
        private SupplierPayment aSupplierPayment = new SupplierPayment();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string dateAsString = DateTime.Now.ToString("yyyy-MM-dd");
                paymentDateTextBox.Value = dateAsString;
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SaveButton(object sender, EventArgs e)
        {
            try
            {
                PurchaseGateway purchaseGateway = new PurchaseGateway();
                int id = purchaseGateway.ReturnPurchaseId(purchaseIdTextBox.Text);

                aManager = new SupplierPaymentManager();
                Purchase aPurchase = new Purchase();
                aPurchase.PurchaseId = id;

                aSupplierPayment.APurchase = aPurchase;
                aSupplierPayment.PaymentDate = paymentDateTextBox.Value;
                aSupplierPayment.PaidAmount = Convert.ToDecimal(paidAmountTextBox.Value);

                string message = aManager.SaveSupplierPayment(aSupplierPayment);
                messageResult.InnerText = message;
                Clear();
            }
            catch (Exception exception)
            {
                messageResult.InnerText = "Supplier payment can not be saved" + "\n" + exception.ToString();
            }
        }

        protected void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        protected void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            purchaseIdTextBox.Text = "";
            dueAmountTextBox.Value = "";
            paymentDateTextBox.Value = "";
            paidAmountTextBox.Value = "";
        }

        protected void DeleteButton(object sender, EventArgs e)
        {
            try
            {
                int supplierPaymentId = Convert.ToInt32(supplierPaymentIdTextBox.Value);
                SupplierPaymentManager supplierPaymentManager = new SupplierPaymentManager();
                string aa = supplierPaymentManager.DeleteSupplierPayment(supplierPaymentId);
                messageResult.InnerText = aa;
            }
            catch (Exception)
            {
                messageResult.InnerText = "Delete Failed";
            }
        }

        protected void purchaseIdTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                PurchaseGateway purchaseGateway = new PurchaseGateway();
                int id = purchaseGateway.ReturnPurchaseId(purchaseIdTextBox.Text);
                SupplierPaymentGateway aSupplierPaymentGateway = new SupplierPaymentGateway();
                decimal totaldue = aSupplierPaymentGateway.GetDuesAmount(id);
                dueAmountTextBox.Value = totaldue.ToString();
            }
            catch (Exception)
            {
                throw;
            }
        }

    }
}